package com.my.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

/**
 * @author WangLuoXin
 * &#064;Date 2024/9/29  10:45
 * web socket 处理器
 **/
public abstract class WebSocketHandler {

    private WebSocketHandler webSocketHandler;

    /**
     * 帧处理器方法
     *
     * @param ctx   套接字
     * @param frame 帧
     */
    protected abstract void received(ChannelHandlerContext ctx, WebSocketFrame frame);


    public void addNext(WebSocketHandler webSocketHandler) {
        this.webSocketHandler = webSocketHandler;
    }

    public void next(ChannelHandlerContext ctx, WebSocketFrame frame) {
        this.received(ctx, frame);
    }

    public WebSocketHandler webSocketHandler() {
        if (null != webSocketHandler) {
            return webSocketHandler;
        }
        return new WebSocketHandler() {
            @Override
            protected void received(ChannelHandlerContext ctx, WebSocketFrame frame) {
            }
        };
    }

    //返回client信息
    public static void sendMessage(ChannelHandlerContext ctx, Object message) {
        ctx.writeAndFlush(message);
    }
}
